package com.cloud.ui;

import com.cloud.DevelopProperties;
import com.cloud.bean.ServiceInfo;
import com.supermap.desktop.controls.ui.controls.labels.SmLinkLabel;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.BrowserUtilities;
import com.supermap.desktop.core.utilties.DefaultValues;
import com.supermap.desktop.core.utilties.FontUtilities;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Locale;

public class PanelServiceInfo extends JPanel {
    private final ServiceInfo serviceInfo;
    private JButton buttonAdd;
    private JLabel labelName;
    private JLabel labelType;
    private SmLinkLabel labelHelpURL;
    private JLabel labelImage;
    private JLabel labelStatus;
    private static final ImageIcon iconStatus_work;
    private static final ImageIcon iconStatus_failed;
    private static final ImageIcon iconStatus_problematic;

    static {
        BufferedImage tempImg = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = tempImg.createGraphics();
        g.setColor(new Color(102, 205, 0));
        g.drawOval(0, 0, 32, 32);
        g.fillOval(0, 0, 32, 32);
        iconStatus_work = new ImageIcon(tempImg.getScaledInstance(8, 8, BufferedImage.TYPE_INT_RGB));

        g.setColor(new Color(255, 69, 0));
        g.drawOval(0, 0, 32, 32);
        g.fillOval(0, 0, 32, 32);
        iconStatus_failed = new ImageIcon(tempImg.getScaledInstance(8, 8, BufferedImage.TYPE_INT_RGB));

        g.setColor(new Color(255, 193, 37));
        g.drawOval(0, 0, 32, 32);
        g.fillOval(0, 0, 32, 32);
        iconStatus_problematic = new ImageIcon(tempImg.getScaledInstance(8, 8, BufferedImage.TYPE_INT_RGB));
        g.dispose();
    }

    public PanelServiceInfo(ServiceInfo serviceInfo) {
        this.serviceInfo = serviceInfo;
        initComponents();
        initLayout();
        registerEvents();
    }

    private void initComponents() {
        labelName = new JLabel(serviceInfo.getName());
        labelName.setToolTipText(serviceInfo.getName());
        labelName.setPreferredSize(DefaultValues.getZoomDimension(138, 26));
        labelName.setHorizontalAlignment(JLabel.LEFT);
        labelName.setFont(new Font("Microsoft YaHei UI ", Font.BOLD, FontUtilities.getDefaultFont().getSize() + 3));
        labelType = new JLabel(serviceInfo.getType().toUpperCase(Locale.ROOT));
        labelType.setHorizontalAlignment(JLabel.LEFT);
        labelType.setPreferredSize(new Dimension(140, 26));
        labelHelpURL = new SmLinkLabel(DevelopProperties.getString("String_Details"));
        labelHelpURL.setHorizontalAlignment(JLabel.LEFT);
        labelImage = new JLabel();
        labelImage.setIcon(serviceInfo.getIcon());
        buttonAdd = new JButton(DevelopProperties.getString("String_Add"));
        labelStatus = new JLabel();
        switch (serviceInfo.getStatus()) {
            case WORK:
                labelStatus.setIcon(iconStatus_work);
                labelStatus.setToolTipText(DevelopProperties.getString("String_Works"));
                break;
            case FAILED:
                labelStatus.setIcon(iconStatus_failed);
                labelStatus.setToolTipText(DevelopProperties.getString("String_Failed"));
                break;
            case PROBLEMATIC:
                labelStatus.setIcon(iconStatus_problematic);
                labelStatus.setToolTipText(DevelopProperties.getString("String_Problematic"));
        }

    }

    private void initLayout() {
        this.setLayout(new GridBagLayout());
        this.add(labelImage, new GridBagConstraintsHelper(0, 0, 1, 2)
                .setAnchor(GridBagConstraints.WEST)
                .setFill(GridBagConstraints.HORIZONTAL)
                .setWeight(0, 1)
                .setInsets(5, 5, 5, 20));
        this.add(labelName, new GridBagConstraintsHelper(1, 0, 2, 1)
                .setFill(GridBagConstraints.HORIZONTAL)
                .setWeight(1, 0)
                .setInsets(5, 0, 0, 5));
        this.add(labelType, new GridBagConstraintsHelper(1, 1, 1, 1)
                .setFill(GridBagConstraints.NONE)
                .setWeight(0, 0)
                .setInsets(5, 0, 5, 0));

        this.add(labelHelpURL, new GridBagConstraintsHelper(2, 1, 1, 1)
                .setFill(GridBagConstraints.NONE)
                .setWeight(1, 0)
                .setInsets(5, 5, 5, 5));
        this.add(labelStatus, new GridBagConstraintsHelper(3, 0, 1, 2)
                .setFill(GridBagConstraints.HORIZONTAL)
                .setWeight(0, 0)
                .setInsets(5, 0, 0, 15));
        this.add(buttonAdd, new GridBagConstraintsHelper(4, 0, 1, 2)
                .setAnchor(GridBagConstraints.CENTER)
                .setFill(GridBagConstraints.HORIZONTAL)
                .setWeight(0, 0)
                .setInsets(5, 0, 5, 5));
    }

    private void registerEvents() {
        labelHelpURL.addActionListener(e -> BrowserUtilities.openUrl(serviceInfo.getHelpURL()));
        buttonAdd.addActionListener(e -> serviceInfo.create());
    }
}
